//
//  CycTextField.m
//  Cycling
//
//  Created by cjg on 2023/3/12.
//

#import "CycTextField.h"

@implementation CycTextField


-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

-(instancetype)init{
    if (self=[super init]) {
        [self setUp];
    }

    return self;
}

-(void)setUp{
    self.backgroundColor = COLOR_APP_BACKGROUND;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = DEFAULT_CORNER_RADIUS;
    self.textColor = COLOR_WHITE;
    self.font = FONT_BODY_16;
    
}

- (BOOL)becomeFirstResponder{
    return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponder{
    self.layer.borderWidth = 0;
    return [super resignFirstResponder];
}

- (void)drawPlaceholderInRect:(CGRect)rect {
    // 计算占位文字的 Size
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName : COLOR_OFFLINE,
                                 NSFontAttributeName : FONT_BODY_16
                                 };
    CGSize placeholderSize = [self.placeholder sizeWithAttributes:attributes];
    [self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes: attributes];
}

//让文字不顶着左右两边
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
    if (self.leftView && self.rightView) {
        return CGRectInset(bounds, 40, 0);
    }
    if (self.leftView) {
        bounds.size.width += 30;
        return CGRectInset(bounds, 50, 0);
    }
    if (self.rightView || self.clearButtonMode) {
        bounds.size.width -= 20;
    }
     return CGRectInset(bounds, 20.0, 0.0);
}

// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
    if (self.leftView && self.rightView) {
        return CGRectInset(bounds, 40, 0);
    }
    if (self.leftView) {
        bounds.size.width += 30;
        return CGRectInset(bounds, 50, 0);
    }
    if (self.rightView || self.clearButtonMode) {
        bounds.size.width -= 20;
    }
     return CGRectInset(bounds, 20.0, 0.0);
}

- (CGRect)clearButtonRectForBounds:(CGRect)bounds{
    CGRect rect = [super clearButtonRectForBounds:bounds];
    if (self.clearButtonMode) {
        rect.origin.x -= 10;
    }
     return rect;
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    CGRect leftRect = [super leftViewRectForBounds:bounds];
    leftRect.origin.x += 20; //右边偏20
    return leftRect;
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
    CGRect rightRect = [super rightViewRectForBounds:bounds];
    rightRect.origin.x -= 10; //左边偏10
    return rightRect;
}

//MARK: - 输入密码模式
- (void)setIsSecureEntryMode:(BOOL)isSecureEntryMode{
    _isSecureEntryMode = isSecureEntryMode;
    if (!isSecureEntryMode) {
        return;
    }
    UIButton *eyeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    eyeButton.backgroundColor = [UIColor clearColor];
    [eyeButton setImage:[UIImage imageNamed:@"pwd_close"] forState:(UIControlStateNormal)];
    [eyeButton setImage:[UIImage imageNamed:@"pwd_open"] forState:UIControlStateSelected];
    eyeButton.titleLabel.font = FONT_BODY_16;
    eyeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [eyeButton addTarget:self action:@selector(eyeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.rightView = eyeButton;
    self.rightViewMode = UITextFieldViewModeAlways;
    self.secureTextEntry = YES;
}

-(void)eyeButtonClick:(UIButton *)btn{
    [btn setSelected:!btn.isSelected];
    self.secureTextEntry = !self.isSecureTextEntry;
}


@end
